package com.BlogAutoTest.commom;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;


import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-10-15
 * Time:13:02
 */
public class AutoTestUtils {
    public static EdgeDriver driver;

    //创建驱动对象
    public static EdgeDriver createDriver() {
        if(driver == null) {
            EdgeOptions options = new EdgeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new EdgeDriver(options);
            //创建隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        }
        return driver;
    }

    //保存屏幕截图
    public static void getScreenShot(String str) throws IOException {
        List<String> list = getTime();
        //dir+filename
        //./指的是当前的项目路径下，也就是BlogAutoTest下
        //./src/test...相对路径
        String filename = "./src/test/java/com/BlogAutoTest/"+list.get(0)+"/" + str + "_" + list.get(1)+".png";
        File srcFile = driver.getScreenshotAs(OutputType.FILE);
        //放到指定位置
        FileUtils.copyFile(srcFile,new File(filename));

    }

    public static List<String> getTime() {
        //文件按照天的维度按文件进行保存
        //文件格式 20230302-123244毫秒
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd-HHmmssSSS");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
        String filename = simpleDateFormat1.format(System.currentTimeMillis());
        String dirname = simpleDateFormat2.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }

    //操作js的confirm弹窗,bool控制是否点击确定，true为点击确定，false为点击取消

//    public static void OperaterJSOfConfirm(EdgeDriver driver,String str,Boolean bool) throws InterruptedException, AWTException {
//        driver.findElement(By.cssSelector(str)).click();
//        Thread.sleep(2500);
//		/*Alert lert = driver.switchTo().alert();
//		Thread.sleep(1000);//睡眠2″，否则可能会提示没有打开的lert错误
//		if(bool){
//			lert.accept();
//		}else{
//			lert.dismiss();
//		}*/
//        //解决上面代码经常出现的“no alert open”异常情况
//
//        Robot robot = new Robot();//在基本屏幕坐标系中构造一个 Robot 对象
//        robot.keyPress(KeyEvent.VK_SPACE);//按下给定的键
//        robot.keyRelease(KeyEvent.VK_SPACE);//释放该键
//    }
}
